import requests
import json
import threading
import os
import random

class Myproxys(object):
	"""docstring for Myproxys"""
	def __init__(self):
		self.proxysurl = "https://raw.githubusercontent.com/fate0/proxylist/master/proxy.list"
		self.ipTest = ""
		self.dir = "data/"

	#验证
	def testIP(self,a):
		try:
			r = requests.get('http://www.baidu.com/', proxies = a, timeout = 30)
			print(a['http'] + ' - ' + str(r.status_code))
			if r.status_code == 200:
				#前后缀设置 默认为   '192.168.1.1:80',
				prefix = ""	#前缀   '
				postfix = "\n"  #后缀 ',\r\n
				self.ipTest += prefix + a['http'] + postfix
				return True
		except:
			print(a['http'] + ' - error')


	def set(self):
		res = requests.get(self.proxysurl)
		data = res.text.split("\n")
		proxys = []
		for item in data:
			if item != "":
				a = json.loads(item)
				proxy_temp = {"http":a['host'] + ":" + str(a['port'])}
				proxys.append(proxy_temp)

		threads = []
		for i in range(100):
			thread = threading.Thread(target=self.testIP,args=[proxys[i]])
			threads.append(thread)
			thread.start()

		for x in threads:
			x.join()
		if not os.path.exists(self.dir):
			os.makedirs(self.dir)

		with open(self.dir+"ip.txt","w") as f:
			f.write(self.ipTest)
		return True

	def get(self):
		with open(self.dir+"ip.txt","r") as f:
			data = f.read().split("\n")
			while 1:
				ipx = random.choice(data)
				if ipx:
					ipxs = {"http":ipx}
					if self.testIP(ipxs):
						return ipx
						break

